package com.atguigu.gulimall.order.vo;

import lombok.Getter;
import lombok.Setter;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 订单确认页需要的数据
 */
public class OrderConfirmVo {
    //收获地址
    @Getter
    @Setter
    private List<MemberAddressVo> address;

    //所有选中的购物项
    @Getter
    @Setter
    private List<OrderItemVo> items;

    //优惠信息
    @Getter
    @Setter
    private Integer integration;

    //防重令牌
    @Getter
    @Setter
    private String orderToken;

    //运费
    @Getter
    @Setter
    private BigDecimal fare;

    public Integer getCount() {
        if (CollectionUtils.isEmpty(items))
            return 0;
        int count = items.stream().mapToInt(item->item.getCount()).sum();
        return count;
    }

    //总价
    public BigDecimal getTotal() {
        if (CollectionUtils.isEmpty(items))
            return BigDecimal.ZERO;
        //计算购物项选中的总价
        BigDecimal reduce = items.stream().map(item -> item.getPrice().multiply(new BigDecimal(item.getCount()))).reduce(BigDecimal.ZERO, BigDecimal::add);
        return reduce;
    }

    //应付价格
    public BigDecimal getPayPrice() {
        //订单总额
        BigDecimal total = getTotal();

        //订单总额减去运费
        BigDecimal subtract = total.subtract(fare);
        return subtract;
    }
}
